package com.ldcc.report_system.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.ui.LoginActivity;
import com.ldcc.report_system.ui.MainActivity;
import com.ldcc.report_system.ui.guide_page.GuidePageActivity;
import com.ldcc.report_system.util.http.ClientRequestManage;
import com.ldcc.report_system.util.http.HttpObserver;
import com.ldcc.report_system.util.http.ServerResponse;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author wwj[yeshengkele@outlook.com]
 * @description 跳转
 */
public class ActivitySkipUtil {

    public static String TAG = "ActivitySkipUtil";
    // 数据标识
    public static final String DATA_ID = "dataID";

    // 跳转界面的编辑标识
    public static final String DATA_EDIT_FLAG = "dataEditFlag";


    /**
     * @param activity 发起跳转的Activity实例
     * @param cls      目标Activity实例
     * @description 简单地Activity的跳转(不携带任何数据)
     */
    public static void skipAnotherActivity(Activity activity,
                                           Class<? extends Activity> cls) {
        Intent intent = new Intent(activity, cls);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * @param dataId   数据id标识
     * @param activity 发起跳转的Activity实例
     * @param cls      目标Activity实例
     * @description 携带数据的Activity的跳转
     */
    public static void skipAnotherActivity(Activity activity,
                                           Class<? extends Activity> cls, String dataId) {
        Intent intent = new Intent(activity, cls);
        // 设置数据
        intent.putExtra(DATA_ID, dataId);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * @param dataId   数据id标识
     * @param activity 发起跳转的Activity实例
     * @param cls      目标Activity实例
     * @description 简单地Activity的跳转(不携带任何数据)
     */
    public static void skipAnotherActivity(Activity activity,
                                           Class<? extends Activity> cls, String dataId, Boolean editFlag) {
        Intent intent = new Intent(activity, cls);
        // 设置数据
        intent.putExtra(DATA_ID, dataId);
        intent.putExtra(DATA_EDIT_FLAG, editFlag);
        activity.startActivity(intent);
        activity.finish();
    }


    /**
     * @param activity       发起跳转的Activity实例
     * @param targetFragment 主界面fragment下标
     * @description 跳转到主页面的目标fragment
     */
    public static void backMainActivityFragment(Activity activity, int targetFragment) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.putExtra(MainActivity.TARGET_FRAGMENT, targetFragment);
        activity.startActivity(intent);
        activity.finish();
    }


    /**
     * @param activity 调用界面
     * @description 检查用户是否登录，如果没有登录发出提示并跳转至登录界面
     */
    public static void checkUserToLogin(AppCompatActivity activity) {
        Context context = ReportApplication.getContext();
        String token = (String) SharedPreferencesUtils.getParam(context, ApplicationConfig.USER_TOKEN_KEY, "");
        Log.e("checkLogin====", "当前token:" + token);
        if (!"".equals(token)) {
            // 如果本地有token就发起请求对比
            ClientRequestManage.getInstance().getUserApi().compareToTaken().subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new HttpObserver<Object>(ReportApplication.getContext(), null) {

                        @Override
                        public void onCodeError(ServerResponse<Object> response_entity) {
                            super.onCodeError(response_entity);
                            ActivitySkipUtil.skipAnotherActivity(activity, LoginActivity.class);
                        }

                        @Override
                        public void onSuccess(ServerResponse<Object> response_entity) {
                        }
                    });
        } else {
            AlertUtil.shortTime("您未登录");
            ActivitySkipUtil.skipAnotherActivity(activity, LoginActivity.class);
        }
    }

    /**
     * 检查用户是否第一次使用 app, 如果第一次使用则跳转到引导页
     * @param activity 发起跳转的Activity实例
     */
    public static void checkFirstUse(AppCompatActivity activity) {
        Context context = ReportApplication.getContext();
        Boolean isFirstUse = false;
        isFirstUse = (Boolean) SharedPreferencesUtils.getParam(context, ApplicationConfig.IS_FIRST_USE, true);
        Log.e(TAG, "checkFirstUse: " + isFirstUse);
        if (isFirstUse != null) {
            if (isFirstUse) {
                ActivitySkipUtil.skipAnotherActivity(activity, GuidePageActivity.class);
            }
        }
    }
}
